/*

 * Copyright 2009 Gal Dolber
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.user.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.dragdrop.client.DragController;
import com.google.gwt.user.dragdrop.client.ResizeController;
import com.google.gwt.user.dragdrop.client.impl.DragImpl;
import com.google.gwt.user.dragdrop.client.impl.ResizeImpl;
import com.google.gwt.user.dragdrop.client.resources.Theme;

public class ResizeDialogBox extends ResizeController {

    private static ResizeDialogBox lastShowed = null;
    
    private HorizontalPanel title = new HorizontalPanel();

    private HTML caption = new HTML();

    private SimplePanel content = new SimplePanel();

    VerticalPanel wrapper = new VerticalPanel();

    private PushButton closeButton = new PushButton();

    public ResizeDialogBox() {
	this(Theme.BLACK);
    }

    public ResizeDialogBox(Theme theme) {
	super(theme);
	super.setWidget(wrapper);
	
	disableResizing();

	select();
	
	closeButton.setStyleName("closeButton");

	this.impl = new ResizeImpl();

	// Prevent overflow of the content
	cellFormatter.getElement(1, 1).getStyle().setProperty("overflow", "hidden");

	wrapper.add(title);
	wrapper.add(content);

	title.getElement().getParentElement().setClassName("TitleWrapper");

	title.add(caption);
	title.setWidth("100%");
	title.setCellWidth(caption, "100%");
	title.add(closeButton);

	closeButton.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		hide();
	    }
	});
	closeButton.setHeight("100%");
	closeButton.setTitle("Close");

	content.setSize("100%", "100%");
	title.setStyleName("Caption");

	caption.setStyleName("Title");

	hide();

	DragController.make(this, new DragImpl() {
	    @Override
	    public void onDragStart(DragController controller) {
		select();
		super.onDragStart(controller);
	    }
	});
	RootPanel.get().add(this, 0, 0);
    }

    public HTML getCaption() {
	return caption;
    }

    @Override
    public Widget getDragDelegate() {
	return caption;
    }

    public String getHTML() {
	return caption.getHTML();
    }

    @Override
    public Widget getResizeDelegate() {
	return wrapper;
    }

    public String getText() {
	return caption.getText();
    }

    public void hide() {
	setVisible(false);
    }

    public void setHTML(String html) {
	caption.setHTML(html);
    }

    public void setPosition(int x, int y) {
	RootPanel.get().setWidgetPosition(this, x, y);
    }

    public void setText(String text) {
	caption.setText(text);
    }

    public void setWidget(Widget w) {
	content.setWidget(w);
    }

    public void show() {
	setVisible(true);
	select();
    }

    /**
     * Select.
     */
    private void select() {
	if (lastShowed!=null) {
	    lastShowed.getElement().getStyle().setZIndex(999);
	}
	lastShowed = this;
	this.getElement().getStyle().setZIndex(1000);
    }
}
